import Request from "@/sdk/pocky-request/index.js"; //引入
/**
 * 网络
 */
const netWork = {
    // 状态
    state: {
		nowNetType:'none',			//当前网络状态,默认无网络
    },
    // 内部派生
    getters: {

    },
    // 同步方法 commit
    mutations: {
		// 设置当前网络状态
		setNetWork(state,_type){
			state.nowType=_type;
			if(_type=='none'){
				uni.showModal({
					title: '错误',
					content: '网络已断开,请检查网络',
					showCancel: false,
					cancelText: '',
					confirmText: '确定',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}else if(_type=='unknown'){
				uni.showToast({
					title: '未知网络类型',
					icon: 'none'
				});
			}else if(_type=='2g'||_type=='3g'||_type=='4g'){
				uni.showToast({
					title: '移动数据网络,请注意流量',
					icon: 'none'
				});
			}else{
				
			}
		}
    },
    // 异步方法 dispatch
    actions: {
		// 获取当前网络状态
		getNetWorkType(context){
			uni.getNetworkType({
				success: (res) => {
					this.commit("setNetWork",res.networkType)
				},
			})
		},
		// 监听网络状态
		listenNetChange(context){
			uni.onNetworkStatusChange( (res)=>{
				if(!res.isConnected){
					this.commit("setNetWork",'none')	
				}else{
					this.commit("setNetWork",res.networkType)	
				}
			    
			});
		}
    }
}

export default netWork;
